<?php
/**
 * 首页
 */
namespace app\api\controller;

use app\common\model\OrderModel;
use app\common\model\Book;
use think\Controller;
use think\Db;
use think\Request;

class Index extends Common
{
    public $captcha;
    public $tempPhone;

    public function _initialize()
    {

    }

    public function test(){
        echo 'testing' . '<br>';

        //print_r(config('goods_main_path'));exit;


    }

    /**
     * 获取首页相关数据
     */
    public function getHomeData()
    {
        //TODO...


        return parent::apiReturnSU();
        //return returnJson(200, '请求成功');
    }

    /**
     * 获取订单数量
     */
    public function getOrderCount()
    {
        if (!session('merchant')) {
            return returnJson(0, '未登录');
            //return parent::apiReturnFA('未登录');
        }
        // 1全部訂單 2待發貨 3待結算 4退款退貨 5成功訂單 6待歸還 7续租
        $tabs = [1,2,3,4,5,6,7];
        foreach ($tabs as $tab) {
            $where = $this->getOrderCountCondition($tab);
            $count[$tab] = orderModel::order_count($where);
            unset($where);
        }
        return returnJson(1, 'ok', $count);
        //return parent::apiReturnSU('ok', $count);
    }


    // ======================================
    // ============  私有方法   =============
    // ======================================

    private function getOrderCountCondition($tab = 1)
    {
        $where['gop.order_status'] = ['eq', 2];
        $where['order.order_merchant_id'] = ['eq', session('merchant.merchant_id')];
        //echo session('merchant.merchant_id');exit;
        $where['order.order_is_delete'] = ['eq', 0];
        switch (intval($tab)) {
            case 2:
                $where['order.order_refund_goods_status'] = ['eq', 0];
                $where['order.order_shipping_status'] = ['eq', 1];
                break;
            case 3:
                $where['order.order_refund_goods_status'] = ['eq', 0];
                $where['order.order_shipping_status'] = ['in', [5,4]];
                break;
            case 4:
                $where['order.order_shipping_status'] = ['in', [2,3,4,5]];
                $where['order.order_refund_goods_status'] = ['eq', 2];
                break;
            case 5:
                $where['order.order_shipping_status'] = ['in', [6,7]];
                break;
            case 6:  //待归还
                $where['order.order_refund_goods_status'] = ['eq', 0];
                $where['order.order_shipping_status'] = ['in', [2,3]];
                break;
            case 7: // 续租订单
                $where['order.order_shipping_status'] = ['eq', 9];
                $where['order.relet_status']    = ['eq', 1];
                break;
        }
        return $where;
    }

    /**
     * 获取借阅订单数量
     */
    public function getBorrowOrderCount()
    {
        if (!session('merchant')) {
            return returnJson(0, '未登录');
        }
        // 1全部订单 2待发货 3待归还 4待结算 5成功订单 6逾期订单 7取消订单
        $tabs = [1,2,3,4,5,6,7];
        foreach ($tabs as $tab) {
            $where = $this->getBorrowOrderCountCondition($tab);
            $book = new Book();
            $count[$tab] = $book->borrowOrderCount($where);
            unset($where);
        }
        return returnJson(1, 'ok', $count);
    }


    // ======================================
    // ============  私有方法   =============
    // ======================================

    private function getBorrowOrderCountCondition($tab = 1)
    {
        switch(intval($tab)){
            //全部
            case 1:
                $where['bo.status']    = ['not in', [9,10]];
                break;
            //待发货
            case 2:
                $where['bo.status']    = ['in', [2]];
                break;
            //待归还
            case 3:
                $where['bo.status']    = ['in',[3,4]];
                $where['bo.rent_end_time']    = ['egt',time()];
                break;
            //待结算
            case 4:
                $where['bo.status']    = ['in', [5]];
                break;
            //成功订单
            case 5:
                $where['bo.status']    = ['in', [7,8,-1]];
                break;
            //逾期订单
            case 6:
                $where['bo.status']    = ['in',[3,4]];
                $where['bo.rent_end_time']    = ['lt',time()];
                break;
            //取消订单
            case 7:
                $where['bo.status']    = ['in',[9,10,11,12]];
                $where['bo.pay_type']    = ['gt',0];
                $where['bo.pay_amount']    = ['gt',0];
                break;
        }
        $where['bo.merchant_id'] = ['eq', session('merchant.merchant_id')];
        $where['bo.origin_id'] = ['eq', 0];
        return $where;
    }

}
